package com.sunday.aomicaichang.adpter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.model.Product;
import com.sunday.aomicaichang.ui.MallFoodDetailActivity;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yun_sheng on 2017/6/1.
 */

public class MallAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context mContext;
    private List<Product> productList;

    public MallAdapter(Context mContext) {
        this.mContext = mContext;

    }
    public void setmData(List<Product> productList){
        this.productList = productList;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mall, parent, false);
        return new ItemViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Product product=productList.get(position);
        ItemViewHolder viewHolder = (ItemViewHolder) holder;
        if(product.getImage()!=null)
            Picasso.with(mContext)
                    .load(product.getImage())
                    .error(R.mipmap.ic_product)
                    .into(viewHolder.img);
        if(product.getName()!=null)
            viewHolder.name.setText(product.getName());
        viewHolder.score.setText(product.getPrice()+"积分");
        viewHolder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, MallFoodDetailActivity.class);
                intent.putExtra("productId",Long.valueOf(product.getId()));
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return productList == null ? 0 : productList.size();
    }


    public class ItemViewHolder  extends RecyclerView.ViewHolder {
        @Bind(R.id.img)
        ImageView img;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.score)
        TextView score;
        @Bind(R.id.item)
        LinearLayout item;
        public ItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}